<template>
  <div>
    <div class="wrapper">
      <Anchor :labelList="labels" :labelWidth="100">
        <div id="row-01" class="line">
          <BreakSpace label="基本信息" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
          <FormPanel ref="form" :list="formList" labelWidth="130" :cols="2" :formType="type" :isSubmitBtn="false" />
          <div :style="{ textAlign: 'right' }">
            <!-- <el-button size="small" type="primary" @click="getInfo">查询</el-button> -->
            <AjaxButton type="primary" :click="getInfo">查询</AjaxButton>
            <AjaxButton size="small" :loading="printBtnLoading" type="primary" :click="printHandle">打印</AjaxButton>
          </div>
        </div>
        <div id="row-02" class="line">
          <BreakSpace label="TEQUIP信息" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
          <FilterTable
            ref="table1"
            columnsRef="jzyCardType1"
            height="auto"
            :columns="columnsTEQUIP"
            :dataSource="TEQUIPList"
            :isSelectColumn="false"
            :onColumnsChange="columns => (this.columnsTEQUIP = columns)"
          ></FilterTable>
        </div>
        <div id="row-03" class="line">
          <BreakSpace label="TOPT信息" :containerStyle="{ marginTop: '10px', marginBottom: '10px' }"></BreakSpace>
          <FilterTable
            ref="table2"
            columnsRef="jzyCardType2"
            height="auto"
            :columns="columnsTOPT"
            :dataSource="TOPTList"
            :isSelectColumn="false"
            :onColumnsChange="columns => (this.columnsTOPT = columns)"
          ></FilterTable>
        </div>
        <div id="row-04" class="line">
          <BreakSpace label="TBAS信息" :containerStyle="{ marginTop: '10px', marginBottom: '10px' }"></BreakSpace>
          <FilterTable
            ref="table3"
            columnsRef="jzyCardType3"
            height="auto"
            :columns="columnsTBAS"
            :dataSource="TBASList"
            :isSelectColumn="false"
            :onColumnsChange="columns => (this.columnsTBAS = columns)"
          ></FilterTable>
          <!-- 打印插件 -->
          <BasePrint ref="print" :data="printData" template="part/report/templateQueryVehicleRelated" />
        </div>
      </Anchor>
    </div>
    <!-- <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeHandler">关闭</el-button>
      <AjaxButton :click="saveHandler" type="primary">保存</AjaxButton>
    </div>-->
  </div>
</template>

<script>
// 数据字典/按钮权限
import { dictionary } from '@/utils/dictMixin';
import { authority } from '@/utils/authMixin';
import { notifyAction } from '@/utils';

import moment from 'moment';

import { excuteCardInfo, getWtsList, getJpList } from '@/api/members/basic/cardType';
import { getInfoByVin } from '@/api/part/report/queryVehicleRelated';
import { async } from 'q';
export default {
  name: 'part_report_queryVehicleRelated',
  mixins: [dictionary, authority],
  props: {
    type: {
      type: String
    },
    formData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.$tableTEQUIP = null;
    this.$tableTTOPT = null;
    this.$tableTBAS = null;
    return {
      labels: this.createAnchorLabels(),
      formList: this.createFormList(),
      columnsTEQUIP: this.createTEQUIPTableColums(),
      TEQUIPList: [],
      columnsTOPT: this.createTOPTTableColums(),
      columnsTBAS: this.createTBASTableColums(),
      TOPTList: [],
      TBASList: [],
      printData: [], // 打印数据
      printBtnLoading: false
    };
  },
  mounted() {
    // 获取 Table 组件对象
    this.$tableTEQUIP = this.$refs.table1.$refs.pageTable;
    this.$tableTTOPT = this.$refs.table2.$refs.pageTable;
    this.$tableTBAS = this.$refs.table3.$refs.pageTable;
    // 初始化方法
    // this.initialHandle();
  },

  methods: {
    // 打印回调函数
    async printHandle() {
      this.printBtnLoading = true;
      // 获取打印数据
      const [err, formData] = await this.$refs.form.GET_FORM_DATA();
      if (err) {
        return this.$notify.error({ title: '提示信息', message: '请检查提交数据' });
      }
      let res = await getInfoByVin(formData);
      this.printBtnLoading = false;
      if (res.resultCode == 200) {
        // KWMENG 数量(需要转换)  NETWR 总价(单价需要除)  MATNR 配件代码 ARKTX 配件名称 还需要计算税额

        this.printData = res.data;
        this.$refs.print.EXCUTE_PRINT();
      } else {
        notifyAction('请选择需要打印的单据', 'warning');
      }
    },
    // 初始化回调函数
    initialHandle() {
      if (this.type === 'edit') {
        // 控制表单项禁用状态
        this.formList.find(x => x.fieldName === 'vClassName').disabled = true;
        // 表单控件值回显
        this.formList.forEach(x => {
          const val = this.formData[x.fieldName];
          if (typeof val === 'undefined') return;
          // 设置回显值
          x.initialValue = val;
          // 控制表单项禁用状态
          if (x.fieldName === 'cPrestoreFavour') {
            this.formList.find(x => x.fieldName === 'nPrestoreAmt').disabled = val === '0' ? true : false;
          }
          if (x.fieldName === 'cStop') {
            this.formList.find(x => x.fieldName === 'dStop').disabled = val === '0' ? true : false;
          }
        });
        // 特殊格式表单项
        this.formList.find(x => x.fieldName === 'nPointStart|nPointEnd').initialValue = [this.formData.nPointStart, this.formData.nPointEnd];
        // 获取数据
        this.getWtsInfo(this.formData.id);
        this.getJpInfo(this.formData.id);
      }
    },
    createAnchorLabels() {
      return [
        {
          title: '基本信息',
          id: 'row-01'
        },
        {
          title: 'TEQUIP信息',
          id: 'row-02'
        },
        {
          title: 'TOPT信息',
          id: 'row-03'
        },
        {
          title: 'TBAS信息',
          id: 'row-04'
        }
      ];
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '底盘号',
          fieldName: 'I_VIN',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入底盘号', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '18位产品代码',
          fieldName: 'PRO_NR',
          readonly: true,
          placeholder: ''
          // style: { width: 'calc(100% - 30px)' },
          // descOptions: {
          //   content: '月'
          // }
        },
        // {
        //   type: 'INPUT',
        //   // label: '积分节点',
        //   fieldName: 'nPointStart',
        //   precision: 2
        // },
        {
          type: 'INPUT',
          label: '生产KAN号',
          fieldName: 'LOG_NR',
          readonly: true,
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '车型年',
          fieldName: 'FZMJ',
          readonly: true,
          placeholder: ''
        },

        {
          type: 'INPUT',
          label: '发动机号',
          readonly: true,
          fieldName: 'ENG_NR',
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '下线时间',
          readonly: true,
          fieldName: 'DATP',
          placeholder: ''
        }
      ];
    },
    createTEQUIPTableColums() {
      return [
        {
          title: '内容',
          dataIndex: 'CONTENT',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    createTOPTTableColums() {
      return [
        {
          title: '类型',
          dataIndex: 'PRNR2',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '名称',
          dataIndex: 'CNAME2',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    createTBASTableColums() {
      return [
        {
          title: '类型',
          dataIndex: 'PRNR3',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '名称',
          dataIndex: 'CNAME3',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },

    // table 组件新增行
    addRecordHandle(type) {
      this[`$table${type}`] && this[`$table${type}`].EXECUTE_INSERT({ nManhaurDisrate: 0, nMaterialDisrate: 0 });
    },
    // table 组件移除记录
    removeRecordHandle(type) {
      this[`$table${type}`] && this[`$table${type}`].EXECUTE_DELETE();
    },
    async getWtsInfo(id) {
      this.$tableTEQUIP.START_LOADING();
      // ajax
      const res = await getWtsList({ id });
      if (res.resultCode === 200) {
        this.TEQUIPList = res.data || [];
      }
      this.$tableTEQUIP.STOP_LOADING();
    },
    async getJpInfo(id) {
      this.$tableTTOPT.START_LOADING();
      // ajax
      const res = await getJpList({ id });
      if (res.resultCode === 200) {
        this.TOPTList = res.data || [];
      }
      this.$tableTTOPT.STOP_LOADING();
    },
    async saveHandler() {
      // 获取表单控件的值，会执行表单校验，遵循错误前置的原则
      const [err, data] = await this.$refs.form.GET_FORM_DATA();
      if (err) return;
      // 执行新增/保存
      const res = await excuteCardInfo({ id: this.formData.id, ...data, CategoryBill: this.TEQUIPList, CategoryBtq: this.TOPTList });
      if (res.resultCode === 200) {
        notifyAction('保存成功！');
        // 需要主页面刷新
        this.closeHandler('refresh');
      }
    },
    async getInfo() {
      const [err, formData] = await this.$refs.form.GET_FORM_DATA();
      if (err) {
        return this.$notify.error({ title: '提示信息', message: '请检查提交数据' });
      }
      let res = await getInfoByVin(formData);
      if (res.resultCode == 200) {
        if (res.data.resultType != 'S') {
          return this.$notify.error({ title: '提示信息', message: res.data.message });
        }
        this.$notify.success({ title: '提示信息', message: res.data.message });
        // // 成功返回200
        // this.closeDialog('refresh'); // 关闭界面
        // return this.$notify({
        //   title: '提示信息',
        //   message: '保存成功',
        //   type: 'success'
        // });
        // res.data.exportParameter;
        this.formList.find(item => item.fieldName === 'PRO_NR').initialValue = res.data.exportParameter.PRO_NR;
        this.formList.find(item => item.fieldName === 'LOG_NR').initialValue = res.data.exportParameter.LOG_NR;
        this.formList.find(item => item.fieldName === 'FZMJ').initialValue = res.data.exportParameter.FZMJ;
        this.formList.find(item => item.fieldName === 'ENG_NR').initialValue = res.data.exportParameter.ENG_NR;
        this.formList.find(item => item.fieldName === 'DATP').initialValue = res.data.exportParameter.DATP;
        this.TEQUIPList = res.data.tableParameter.IT_TEQUIP;
        this.TOPTList = res.data.tableParameter.IT_TOPT;
        this.TBASList = res.data.tableParameter.IT_TBAS;
        // console.log(res);
      }
    },
    // 关闭面板
    closeHandler(type) {
      this.$emit('close', false, type);
    }
  }
};
</script>

<style lang="less" scoped>
.wrapper {
  height: calc(100vh - 135px);
}
</style>
